//
//  LiveViewController.m
//  LOL视频播放
//
//  Created by 花花蔡 on 2016/12/14.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

#import "LiveViewController.h"
#import "Public.h"
#import "LiveCollectionViewCell.h"
#import "NetWorkingTool.h"
#import "URL.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "VideoPlayViewController.h"
@interface LiveViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *dataArray;


@end

@implementation LiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadMoreData];
    self.title = @"直播";
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 加载数据
- (void)loadMoreData {
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //
        [[NetWorkingTool sharedTools] requestMethod:GET WithURL:[NSString stringWithFormat:@"%@",ZhanQiURL] parameters:nil callBack:^(id data, NSError *error) {
            //
            if (error) {
                NSLog(@"%@",error);
                [self.collectionView .mj_header endRefreshing];
            }
            _dataArray = [LiveModel mj_objectArrayWithKeyValuesArray:data];
            [self.collectionView reloadData];
            [self.collectionView.mj_header endRefreshing];
            
        }];
    }];
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark - collectionView 代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LiveModel *model = _dataArray[indexPath.row];
    
    VideoPlayViewController *playerVC = [[VideoPlayViewController alloc] init];
    playerVC.urlStr = model.playurl;
    [self presentViewController:playerVC animated:YES completion:nil];
}
#pragma mark - 懒加载

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.itemSize = CGSizeMake(screen_width/2.21, screen_width/2.21);
        flowLayout.minimumLineSpacing = -15;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[LiveCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}


@end
